package top.xchch.workdemo.func05;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程同步方式五: java 服务编排CompletableFuture
 *
 * @author sai.zhang
 * @date 2021-10-17 23:29
 */
public class Main {
    private static ExecutorService es = Executors.newFixedThreadPool(4);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        CompletableFuture<Integer> taskResult = CompletableFuture.supplyAsync(() -> {
            return new ThirdObj().task();
        }, es);

        System.out.println(taskResult.get());
        System.out.println("使用时间: "+ (System.currentTimeMillis() - start) + " ms");

        es.shutdown();
    }
}

class ThirdObj {

    public Integer task() {
        return fibo(36);
    }

    private synchronized int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}

